package name.nano.server;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import name.nano.client.TeamTacticService;
import name.nano.client.dto.LocationInTime;
import name.nano.client.dto.PlayerTactic;
import name.nano.client.dto.TeamTactic;
import name.nano.client.enums.GameMode;
import name.nano.client.utils.Coordinate;


import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class TeamTacticServiceImpl extends RemoteServiceServlet implements TeamTacticService {
	//String tacticName="'Stack3'";
	@Override
	public TeamTactic getTeamTactic(String tacticName) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
        	TeamTacticDAO teamTactic = getTeamTacticDAO(tacticName, pm);
            if (teamTactic==null) {
				teamTactic = getStackTactic(tacticName);
				pm.makePersistent(teamTactic);
			}
            return	teamTactic.getDTO();
        } finally {
            pm.close();
        }
		
	}
	
	public Boolean storeTeamTactic(String tacticName,TeamTactic tactic) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			
        
        	TeamTacticDAO teamTactic = getTeamTacticDAO(tacticName, pm);
            
            if (teamTactic==null) {
				teamTactic = getStackTactic(tacticName);
			}
            
            teamTactic.setPlayerWithDisk(tactic.getPlayerWithDisk());
            
            
            PlayerTactic[] playersTactics = tactic.getPlayersTactic();
            for (int i = 0; i < playersTactics.length; i++) {
				PlayerTactic playerTactic = playersTactics[i];
				PlayerTacticDAO storedTactic=teamTactic.getPlayersTactic().get(i);
				
				Coordinate startLocation = playerTactic.getStartLocation();
				Coordinate startLocation2 = storedTactic.getStartLocation();
				startLocation2.setX(startLocation.x);
				startLocation2.setY(startLocation.y);
				storedTactic.setMoves(playerTactic.getMoves());
		
			}
            //teamTactic.setPlayerList(newPlayerTactics);
            pm.makePersistent(teamTactic);
            return	Boolean.TRUE;
            
        } finally {
            pm.close();
        }
		
	}

	private TeamTacticDAO getTeamTacticDAO(String tacticName,
			PersistenceManager pm) {
		Query query = pm.newQuery("select from name.nano.server.TeamTacticDAO where m_tacticName == tacticName");
		query.declareParameters("String tacticName");
		List<TeamTacticDAO> list=(List<TeamTacticDAO>) query.execute(tacticName);  
		TeamTacticDAO teamTactic=null;
		if (list.size()>0) {
			//teamTactic=new TeamTactic(pm.detachCopy(teamTactic));
			teamTactic = list.get(0);
		}
		return teamTactic;
	}
	


	private TeamTacticDAO getStackTactic(String name) {
		TeamTacticDAO result=new TeamTacticDAO();
		result.setTacticName(name);
		ArrayList<PlayerTacticDAO> list = new ArrayList<PlayerTacticDAO>();
		int dx = (int) (result.getFieldSize().x/(double)3);
		int x=(int) (0.5*dx);
		
		for (int i=0;i<3;i++){
			PlayerTacticDAO offencePlayer = new PlayerTacticDAO(i,new Coordinate(x,result.getBaselineOffence()),null,GameMode.OFFENCE,"#FF0000");
			list.add(offencePlayer);
			x=x+dx;
		}
		int dStack=4;
		int y=0;
		for (int i=3;i<7;i++){
			PlayerTacticDAO offencePlayer = new PlayerTacticDAO(i,new Coordinate(result.getFieldSize().x*0.5,result.getFieldSize().y/3+y),null,GameMode.OFFENCE,"#FF0000");
			ArrayList<LocationInTime> lit=new ArrayList<LocationInTime>();
			//lit.add(new LocationInTime(1,1,0));
			offencePlayer.setMoves(lit);
			list.add(offencePlayer);
			y=y+dStack;
		}
		
		
		result.setPlayerWithDisk(1);
		dx = (int) (result.getFieldSize().x/(double)8);
		x=dx;
		for (int i=0;i<7;i++){
			PlayerTacticDAO defencePlayer = new PlayerTacticDAO(i+7,new Coordinate(x,result.getBaselineDefence()),null,GameMode.DEFENCE,"#0000FF");
			list.add(defencePlayer);
			defencePlayer.setOpponentNumber(i);
			x=x+dx;
		}
		result.setPlayerList(list);
		return result;
	}
}
